package com.nmrbt.pojo.test.generator;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.net.URL;
import java.util.Arrays;

public class Generator {

    private static String AUTHOR = System.getProperty("user.name");
    private static String OUTPUT_DIR;
    private static String DRIVER_NAME = "com.mysql.cj.jdbc.Driver";
    private static String URL = "jdbc:mysql://192.168.1.230:3306/nmrbt_test_db?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=true";
    private static String USERNAME = "root";
    private static String PASSWORD = "nmrbt666";

    private static String[] TABLE_NAMES = { "export"};
    private static String[] TABLE_PREFIX = { "" };

    static {
        URL resource = Generator.class.getClassLoader().getResource("");
        OUTPUT_DIR = resource.getPath().replace("out/test/classes/", "") + "generator";

    }

    public static void main(String[] args) {
        //全局配置
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setAuthor(AUTHOR);
        globalConfig.setSwagger2(false);
        globalConfig.setActiveRecord(false);
        globalConfig.setOutputDir(OUTPUT_DIR);
        globalConfig.setFileOverride(true);
        globalConfig.setIdType(IdType.INPUT);
        globalConfig.setServiceName("%sService");
        globalConfig.setBaseResultMap(true);
        globalConfig.setBaseColumnList(true);
        globalConfig.setOpen(false);
        globalConfig.setDateType(DateType.ONLY_DATE);
        //数据源配置
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setDbType(DbType.MYSQL);
        dataSourceConfig.setDriverName(DRIVER_NAME);
        dataSourceConfig.setUrl(URL);
        dataSourceConfig.setUsername(USERNAME);
        dataSourceConfig.setPassword(PASSWORD);
        //策略配置
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig.setCapitalMode(true);
        strategyConfig.setTablePrefix(TABLE_PREFIX);
        strategyConfig.setRestControllerStyle(true);
        strategyConfig.setNaming(NamingStrategy.underline_to_camel);
        strategyConfig.setInclude(TABLE_NAMES);
        strategyConfig.setEntityTableFieldAnnotationEnable(true);
        strategyConfig.setEntityColumnConstant(true);
        strategyConfig.setEntityLombokModel(true);
        strategyConfig.setLogicDeleteFieldName("IS_VALID");
        strategyConfig.setTableFillList(Arrays.asList(new TableFill("CREATED_BY", FieldFill.INSERT), new TableFill("LAST_MODIFIED_BY", FieldFill.UPDATE)));
        //包名策略配置
        PackageConfig packageConfig = new PackageConfig();
        packageConfig.setParent("com.nmrbt.pojo")
                .setMapper("dao.bi")
                .setEntity("domain.bi")
                .setController("controller")
                .setService("service")
                .setServiceImpl("service.impl")
                .setXml("dao");
        //整合配置
        AutoGenerator ag = new AutoGenerator();
        ag.setGlobalConfig(globalConfig)
                .setDataSource(dataSourceConfig).
                setPackageInfo(packageConfig).
                setStrategy(strategyConfig);
        ag.execute();
    }

}
